package tum0r.json;

import java.util.Date;

/**
 * 工程: XJSON<br>
 * 包: tum0r.json<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/9/15 14:14<br>
 * <br>
 */
class JSONNode {
    public String key = null;
    public Object value = null;
    public JSONNode rootNode = null;
    public JSONNode leftNode = null;
    public int leftHeight = 0;
    public JSONNode rightNode = null;
    public int rightHeight = 0;

    public JSONNode copy() {
        JSONNode result = new JSONNode();
        result.key = key;
        result.value = value;

        if (this.rootNode != null) {
            result.rootNode = this.rootNode.copy();
        }
        if (this.leftNode != null) {
            result.leftHeight = this.leftHeight;
            result.leftNode = this.leftNode.copy();
        }
        if (this.rightNode != null) {
            result.rightHeight = this.rightHeight;
            result.rightNode = this.rightNode.copy();
        }
        return result;
    }

    private boolean equals(JSONNode node, boolean ignoreCase) {
        boolean result = false;
        if (node != null && node.key != null && node.value != null && key != null && value != null) {
            switch (ignoreCase ? 1 : 0) {
                case 1 -> result = key.equalsIgnoreCase(node.key) && value.equals(node.value);
                case 0 -> result = key.equals(node.key) && value.equals(node.value);
            }
        }
        return result;
    }

    public boolean equals(JSONNode node) {
        return equals(node, false);
    }

    public boolean equalsIgnoreKeyCase(JSONNode node) {
        return equals(node, true);
    }

    public Object getObject() {
        return value;
    }

    public byte getByte() {
        return (byte) getLong();
    }

    public short getShort() {
        return (short) getLong();
    }

    public int getInt() {
        return (int) getLong();
    }

    public long getLong() {
        long result = 0L;
        Object temp = getObject();
        if (temp != null) {
            if (temp instanceof Date) {
                result = ((Date) temp).getTime();
            } else if (temp instanceof Byte || temp instanceof Short || temp instanceof Integer || temp instanceof Long) {
                result = Long.parseLong(temp.toString());
            }
        }
        return result;
    }

    public Date getDate() {
        return new Date(getLong());
    }

    public float getFloat() {
        float result = 0.0f;
        Object temp = getObject();
        if (temp != null) {
            if (temp instanceof Float || temp instanceof Double) {
                result = (float) temp;
            }
        }
        return result;
    }

    public double getDouble() {
        double result = 0.0;
        Object temp = getObject();
        if (temp != null) {
            if (temp instanceof Float || temp instanceof Double) {
                result = (double) temp;
            }
        }
        return result;
    }

    public boolean getBoolean() {
        boolean result = false;
        Object temp = getObject();
        if (temp != null) {
            if (temp instanceof Boolean) {
                result = (boolean) temp;
            } else if (temp instanceof Integer) {
                result = (int) temp != 0;
            }
        }
        return result;
    }

    public char getChar() {
        char result = 0;
        Object temp = getObject();
        if (temp != null) {
            if (temp instanceof Character) {
                result = (char) temp;
            }
        }
        return result;
    }

    public String getString() {
        String result = null;
        Object temp = getObject();
        if (temp != null) {
            result = temp.toString();
        }
        return result;
    }

    public JSONObject getJSONObject() {
        JSONObject result;
        Object temp = getObject();
        if (temp instanceof JSONObject) {
            result = (JSONObject) temp;
        } else {
            result = JSONObject.parse(temp);
        }
        return result;
    }

    public JSONArray getJSONArray() {
        JSONArray result;
        Object temp = getObject();
        if (temp instanceof JSONArray) {
            result = (JSONArray) temp;
        } else {
            result = JSONArray.parse(temp);
        }
        return result;
    }

    public JSON getJSON() {
        JSON result;
        Object temp = getObject();
        if (temp instanceof JSON) {
            result = (JSON) temp;
        } else {
            result = JSONArray.parse(temp);
        }
        return result;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        Object temp = getObject();
        if (temp != null) {
            if (temp instanceof Date || temp instanceof Byte || temp instanceof Short || temp instanceof Integer || temp instanceof Long) {
                result.append(getLong());
            } else if (temp instanceof Float) {
                result.append(getFloat());
            } else if (temp instanceof Double) {
                result.append(getDouble());
            } else if (temp instanceof Boolean) {
                result.append(getBoolean());
            } else if (temp instanceof Character) {
                result.append(getChar());
            } else if (temp instanceof JSONObject) {
                result.append(getJSONObject());
            } else if (temp instanceof JSONArray) {
                result.append(getJSONArray());
            } else if (temp.getClass().isArray()) {
                result.append(JSONArray.parse(temp));
            } else {
                String valueTemp = getString();
                if (valueTemp != null) {
                    result.append("\"");
                    result.append(valueTemp);
                    result.append("\"");
                } else {
                    result.append("null");
                }
            }
        } else {
            result.append("null");
        }
        return result.toString();
    }
}
